package com.gxy.learn.design.model.create.factory.confg_factory;

import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * @description: 咖啡工厂
 * @author: gaoxueyong
 * @Date: 2022/11/7 11:06
 */
@Slf4j
public class CoffeeFactory {
    private static final Map<String, Coffee> beansMap = new HashMap<>();

    static {
        Properties properties = new Properties();
        InputStream inputStream = CoffeeFactory.class.getClassLoader().getResourceAsStream("config_factory_beans.properties");
        try {
            properties.load(inputStream);
            Set<String> strings = properties.stringPropertyNames();
            for (String key : strings) {
                Class<?> aClass = Class.forName(properties.getProperty(key));
                Coffee coffee = (Coffee) aClass.newInstance();
                beansMap.put(key, coffee);
            }
        } catch (Exception e) {
            log.error("err ", e);
        }
    }

    public static Coffee getCoffee(String name) {
        return beansMap.get(name);
    }

}
